<?php
class mysqlclass {
	private $db_id;
	private $querynum = 0;
	private $dbhost="localhost";
	private $dbuser="root";
	private $dbpw;
	private $dbname;
	private $connected;
	var $pconnect=0;
	var $drop_table;
	function mysqlclass($dbhost,$dbuser,$dbpw,$dbname,$pconnect=0)
	{
		$this->dbhost=$dbhost;
		$this->dbuser=$dbuser;
		$this->dbpw=$dbpw;
		$this->dbname=$dbname;
		$this->pconnect=$pconnect;
	}
	function connect() {
		if($this->pconnect) {
			if(!$this->db_id=@mysql_pconnect($this->dbhost, $this->dbuser, $this->dbpw)) {
				$this->mysql_errormsg('Can not connect to MySQL server');
			}
		} else {
			if(!$this->db_id=@mysql_connect($this->dbhost, $this->dbuser, $this->dbpw)) {
				$this->mysql_errormsg('Can not connect to MySQL server');
			}
		}
		@mysql_query("set names 'utf8'");
		@mysql_select_db($this->dbname);
	}

	function select_db($dbname) {
		return @mysql_select_db($dbname,$this->db_id);
	}

	function query($sql,$bug=false) {	
		if($this->connected!=true)
		{
			$this->connect();
		}
		if(!($query = @mysql_query($sql,$this->db_id)) && $bug==false) {
				$this->mysql_errormsg('MySQL Query Error', $sql);
		}	
		if($query==false && $bug===2)
		{
			echo "sqlerr->".$sql."<br>";
		}
		$this->querynum++;		
		return $query;
	}
	
	function fetch_array($query, $result_type = MYSQL_ASSOC) {
		return @mysql_fetch_array($query, $result_type);//MYSQL_ASSOC, MYSQL_NUM, and MYSQL_BOTH. 
	}

	function fetch_row($query) {
		$query = @mysql_fetch_row($query);
		return $query;
	}
	
	function num_rows($query) {
		$query = @mysql_num_rows($query);
		return $query;
	}

	function insert_id() {
		$id = @mysql_insert_id();
		return $id;
	}
	
	function free_result($rs) {
		return @mysql_free_result($rs);
	}
	
	function list_tables() {
		return @mysql_list_tables($this->dbname);
	}
	
	function list_fields($table) {
		return @mysql_list_fields($this->dbname,$table);
	}
	
	function num_fields($query) {
		return @mysql_num_fields($query);
	}
	
	function field_name($fields,$i) {
		return @mysql_field_name($fields,$i);
	}
	
	function field_flags($fields,$i) {
		return @mysql_field_flags($fields,$i);
	}
	
	function field_len($fields,$i) {
		return @mysql_field_len($fields,$i);
	}
	
	function field_type($fields,$i) {
		return @mysql_field_type($fields,$i);
	}
	
	function close() {
		return @mysql_close();
	}	

	function affected_rows() {
		return @mysql_affected_rows();	
	}

	function error() {
		return @mysql_error();
	}

	function errno() {
		return @intval(mysql_errno());
	}
	
	function stat()
	{
		return @mysql_stat();
	}

	function result($query, $row) {
		$query = @mysql_result($query, $row);
		return $query;
	}
	
	function mysql_errormsg($message = '', $sql = '') {
		if($message=="Can not connect to MySQL server")
		{
			echo $message."!<br><a href='javascript:history.back(-1);'>Back</a>";			
		}
		echo "SERVER Busy. Please try again later!<br><a href='javascript:history.back(-1);'>Back</a>";
		
		 $f=@fopen("log-error.txt","a");
		 if($f)
		 {
			$logdata="[".date("d-m-Y/h-i")."-".$_SERVER['REMOTE_ADDR']."] ";
			$logdata.=$message." =>".$sql." ";
			$logdata.="=>".$_SERVER['REQUEST_URI'].">\r\n\r\n";
			@fwrite($f,$logdata);
			@fclose($f);
		 }
		exit();
	}
	
	function Restore($data)
	{
		$arr=explode(";",$data);
		foreach($arr as $i =>$value)
		{
			$value=trim($value);
			$value=preg_replace("((--)(.*))","",$value);
			if(empty($value))continue;
			//echo $value."<br>";
			$this->query($value,2);
		}
	}
	
	function saveconfigdb($url,$host,$user,$pass,$namedb,$first_table)
	{
		$f=@fopen($url,"w");
		if($f)
		{
			$data="<"."?\n $"."db_host='$host';\n $"."db_name='$namedb';\n $"."db_username='$user';\n $"."db_password='$pass';\n";
			$data.=" $"."first_table='$first_table'; \n ?".">";
			@fwrite($f,$data);
			@fclose($f);
		}else
		{
			echo "can't open file config";
		}
	}
	
	function Backup($tables=null,$filesave=null,$encode=false,$down=true,$drop_table=false)
	{
		$this->drop_table=$drop_table;
		if(!is_array($tables))
		{			
			$tables=array();
			$q_tb = $this->list_tables();		
			while($t = $this->fetch_array($q_tb,MYSQL_BOTH))
			{
				array_push($tables, $t[0]);
			}
		}
		
		$exported="######### create by trinhpn 30-1-2010##############\n\n";
		foreach($tables as $table)
		{
			$header = $this->create_header($table);
			$data = $this->get_data($table);
			$exported .= "\n\n$header" . $data . "\n";
		}
		
		if($encode)$exported = @gzencode($exported);
		
		if(!empty($filesave))
		{
			$f=@fopen($filesave,"w");
			@fwrite($f,$exported);
			@fclose($f);
			return;
		}else if($down==false)
		{
			return $exported;
		}else
		{
			$size = strlen($exported);
			if($encode)
			{
				header("Content-disposition: attachment; filename=bkdb_".date("Y-m-d").".sql.gz");
			}else
			{
				header("Content-disposition: attachment; filename=bkdb_".date("Y-m-d").".sql");
			}
			header("Content-Length: $size");
			echo $exported;
		}
	}

	function create_header($table)
	{		
		$h = "###################\n# Dumping table $table\n###################\n";
		if($this->drop_table)
		{
			$h .= "DROP TABLE IF EXISTS `$table`;\n";
		}
		$row=$this->fetch_row($this->query('SHOW CREATE TABLE '.$table));
		$h.=str_replace("CHARSET=latin1","CHARSET=utf8",$row[1]);
		$h.=";\n\n";
		return($h);
	}

	function get_data($table)
	{
		$d = null;
		$data = $this->query("SELECT * FROM `" . $table . "` WHERE 1");
		
		while($cr = $this->fetch_array($data, MYSQL_NUM))
		{
			$d .= "INSERT INTO `" . $table . "` VALUES (";

			for($i=0; $i<sizeof($cr); $i++)
			{
				$d .= "'".mysql_escape_string($cr[$i])."',";
			}

			$d = substr($d, 0, strlen($d) - 1);
			$d .= ");\n";
		}

		return($d);
	}		
}


$dbsql = new mysqlclass($db_host,$db_username,$db_password,$db_name);

unset($db_host,$db_username,$db_password,$db_name);

?>